package com.ruoyi.web.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.elasticsearch.domain.EsSkuInfo;
import com.ruoyi.common.elasticsearch.domain.SearchParam;
import com.ruoyi.common.elasticsearch.domain.SearchResult;
import com.ruoyi.common.elasticsearch.service.ElasticSearchService;
import com.ruoyi.common.oms.entity.YytOmsCartItem;
import com.ruoyi.common.oms.service.YytOmsCartItemService;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.pms.service.YytPmsSkuInfoService;
import com.ruoyi.utils.LoginUtils;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName BaseController
 * @Description
 * @Author huangshun
 * @Date 2021/12/18 16:34
 */
@RestController
@RequestMapping("business")
public class BusinessController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ElasticSearchService elasticSearchService;
    @Autowired
    private YytPmsSkuInfoService yytPmsSkuInfoService;
    @Autowired
    private YytOmsCartItemService yytOmsCartItemService;

//
//    @GetMapping("search")
//    public ResponseResult search(SearchParam searchParam){
//        SearchResult searchResult = elasticSearchService.searchEs(searchParam);
//        return ResponseResult.success(searchResult);
//    }
    @GetMapping("search")
    public ResponseResult search(SearchParam searchParam){
        SearchResult searchResult = elasticSearchService.searchEs(searchParam);
        return ResponseResult.success(searchResult);
    }
    /**
     * 根据skuId获取详情信息
     */
    @GetMapping("/getSkuInfo")
    public ResponseResult getSkuInfo(@RequestParam("id") String skuId) {
        return ResponseResult.success(yytPmsSkuInfoService.getInfo(skuId));
    }

    /**
     * 获取用户购物车商品数量
     */
    @GetMapping("/getShopCartNum")
    public ResponseResult getShopCartNum() {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        LambdaQueryWrapper<YytOmsCartItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytOmsCartItem::getMemberId, loginUser.getId());
        return ResponseResult.success(yytOmsCartItemService.count(wrapper));
    }

    /**
     * 加入购物车
     */
    @PostMapping("/addShopCart")
    public ResponseResult addShopCart(@RequestBody YytOmsCartItem cartItem) {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        cartItem.setMemberId(loginUser.getId());
        if(loginUser.getNickname()!=null){
            cartItem.setMemberNickname(EmojiParser.parseToAliases(loginUser.getNickname()));
        }else{
            cartItem.setMemberNickname(loginUser.getNickname());
        }
        cartItem.setCreateBy(loginUser.getUsername());
        yytOmsCartItemService.addShopCart(cartItem);
        return ResponseResult.success();
    }
    /**
     * 获取用户购物车商品
     */
    @GetMapping("/getShopCart")
    public ResponseResult getShopCart() {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        LambdaQueryWrapper<YytOmsCartItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytOmsCartItem::getMemberId, loginUser.getId());
        return ResponseResult.success(yytOmsCartItemService.list(wrapper));
    }
    /**
     * 修改购物车商品数量
     */
    @GetMapping("/setShopSize")
    public ResponseResult setSopSize(HttpServletRequest request) {
        String num = request.getParameter("num");
        String id = request.getParameter("id");
        YytOmsCartItem yytOmsCartItem = new YytOmsCartItem();
        yytOmsCartItem.setId(id);
        yytOmsCartItem.setQuantity(Integer.parseInt(num));
        return ResponseResult.success(yytOmsCartItemService.updateById(yytOmsCartItem));
    }
    /**
     * 清空购物车
     */
    @GetMapping("/clearCart")
    public ResponseResult clearCart() {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        LambdaQueryWrapper<YytOmsCartItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytOmsCartItem::getMemberId, loginUser.getId());
        return ResponseResult.success(yytOmsCartItemService.remove(wrapper));
    }



    /**
     * 根据分类搜索商品
     * @param searchParam
     * @return
     */
    @GetMapping("/searchClass")
    public ResponseResult searchClass(SearchParam searchParam){
        SearchResult searchResult = elasticSearchService.searchEs(searchParam);
        for (int i = 0; i < searchResult.getProducts().size(); i++){
            YytPmsSkuInfo yytPmsSkuInfo = yytPmsSkuInfoService.getById(searchResult.getProducts().get(i).getSkuId());
            searchResult.getProducts().get(i).setSaleCount(Integer.parseInt(yytPmsSkuInfo.getSaleCount()));
            searchResult.getProducts().get(i).setInventory(yytPmsSkuInfo.getInventory());
            searchResult.getProducts().get(i).setProductPic(yytPmsSkuInfo.getSkuDefaultImg());
            searchResult.getProducts().get(i).setShopId(yytPmsSkuInfo.getShopId());
            searchResult.getProducts().get(i).setPrice(yytPmsSkuInfo.getPrice());
            searchResult.getProducts().get(i).setProductName(yytPmsSkuInfo.getSkuName());
            searchResult.getProducts().get(i).setIsSelect(false);
        }
        return ResponseResult.success(searchResult);
    }

}
